<template>
	<div class="form-area">
		<el-form ref="addForm" label-width="140px" style="width: 100%; height:auto;">
			<!-- 商品信息 -->
			<div class="form-title">
				<h3>规格属性</h3>
			</div>

			<!-- 添加规格属性 -->
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="规格属性" prop="">
						<div style="display: flex;">
							<el-button type="warning" plain @click="addSkuProperty" v-if="!isDetails">添加规格</el-button>
							<span style="margin-left: 20px;">提示：最多添加2组商品规格，第一组商品规格可添加图片</span>
						</div>

						<div>
							<!-- 规格模板 -->
							<div class="sku-list" v-for="(item, index) of addSkuPropertyList" :key="index">
								<div class="form-row">
									<el-form-item :label="'规格名称' + (index + 1)" label-width="80px">
										<el-input v-model="item.propertyName" style="width: 200px; margin-right: 10px;" readonly v-if="isDetails || item.propertyId"></el-input>
										<el-input v-model="item.propertyName" style="width: 200px; margin-right: 10px;" placeholder="请输入规格名称" @blur="addPropertyName(item, index)" v-else></el-input>

										<el-button type="danger" icon="el-icon-delete" circle @click="delectPropertyName(item, index)" v-if="!isDetails"></el-button>
									</el-form-item>
								</div>

								<div>
									<el-form-item label="规格值" label-width="80px">
										<div class="sku-box">
											<!-- 已添加的规格 -->
											<el-tag
												v-for="(sku, skuIndex) of item.skuPropertyValueVoList"
												:key="skuIndex"
												:disable-transitions="false"
												:closable="!isDetails"
												@close="delectSku(sku, index, skuIndex)"
											>
												<span>{{sku.propertyValue}}</span>
												<el-image :src="sku.thumbImage" style="width: 20px; height: 20px;" v-if="sku.thumbImage">
													<i class="el-icon-picture-outline"></i>
												</el-image>
											</el-tag>

											<!-- 添加规格的输入框 -->
											<div class="sku-input" v-if="item.showInput">
												<el-input :ref="'skuInput'+index" class="input-style" v-model="item.inputValue" placeholder="规格值"></el-input>

												<!-- <div class="sku-pic-upload" v-if="index == 0">
													<el-upload
														:ref="'skuImage'+index"
														:limit="1"
														:action="getUploadUrl()"
														:headers="headerParams"
														:auto-upload="false"
														:file-list="fileList"
														list-type="picture-card"
														:on-change="checkPicture"
														:on-success="params => handleUploadSuccess(params, index)"
														:before-remove="handleRemove"
													>
														<i class="el-icon-picture-outline"></i>
													</el-upload>
												</div> -->
												
												<div class="sku-pic-upload" v-if="index == 0">
													<el-upload
														:ref="'skuImage'+index"
														list-type="picture-card"
														:file-list="fileList"
														:limit="1"
														:accept="'image/*'"
														action=""
														:http-request="(file) => getQiniuToken(file, index)"
														:before-upload="previewImage"
														:before-remove="handleRemove"
													>
														<i class="el-icon-picture-outline"></i>
													</el-upload>
												</div>

												<el-button class="button-style" type="warning" plain size="small" @click="saveSku(item, index)">保存</el-button>
											</div>

											<!-- 添加按钮 -->
											<el-button class="button-style" size="small" @click="openSkuInput(item, index)" v-if="!item.showInput && !isDetails">
												添加规格值
											</el-button>
										</div>
									</el-form-item>
								</div>
							</div>
						</div>
					</el-form-item>
				</el-col>
			</el-row>

			<!-- 规格明细 -->
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="规格明细">
						<el-alert title="注意：请注意合理设置价格" type="info" style="width: 400px; margin-bottom: 10px;"></el-alert>

						<!-- 商品SKU表格 -->
						<div v-if="isTable">
							<!-- 批量设置按钮 -->
							<div class="table-header" v-if="!isDetails">
								<div>
									<el-button type="warning" plain size="medium" @click="openBatchPricePurchase">批量设置参考进货价</el-button>
									<el-button type="warning" plain size="medium" @click="openBatchPriceShow">批量设置原价</el-button>
									<el-button type="warning" plain size="medium" @click="openBatchPrice">批量设置销售价</el-button>
									<el-button type="warning" plain size="medium" @click="openBatchMemberPrice">批量设置会员价</el-button>
									<el-button type="warning" plain size="medium" @click="openBatchCouponAmount">批量设置券抵扣金额</el-button>
								</div>

								<div>
									<el-button type="primary" size="medium" @click="refreshTableData">刷新表格</el-button>
								</div>
							</div>

							<el-table :data="skuTableData" :span-method="objectSpanMethod" border style="width: 100%;">
								<el-table-column label="图片" width="84" align="center">
									<template slot-scope="scope">
										<el-image :src="scope.row.thumbImage" style="width: 60px; height: 60px;"></el-image>
									</template>
								</el-table-column>
								<el-table-column v-for="header in headerDatas" :prop="header.prop" :label="header.label" :key="header.label" align="center"></el-table-column>
								<el-table-column prop="productBarCode" label="条码" align="center">
									<template slot-scope="scope">
										<el-input v-model="scope.row[scope.column.property]" style="width:100%" placeholder="请输入条码" :readonly="isDetails"></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="pricePurchase" label="参考进货价(元)" width="150" align="center">
									<template slot-scope="scope">
										<el-input
											v-model="scope.row[scope.column.property]"
											style="width:100%"
											placeholder="请输入价格"
											:readonly="isDetails"
										></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="priceShow" label="原价(元)" width="150" align="center">
									<template slot-scope="scope">
										<el-input v-model="scope.row[scope.column.property]" style="width:100%" placeholder="请输入价格" :readonly="isDetails"></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="price" label="销售价(元)" width="150" align="center">
									<template slot-scope="scope">
										<el-input v-model="scope.row[scope.column.property]" style="width:100%" placeholder="请输入价格" :readonly="isDetails"></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="memberPrice" label="会员价(元)" width="150" align="center">
									<template slot-scope="scope">
										<el-input v-model="scope.row[scope.column.property]" style="width:100%" placeholder="请输入价格" :readonly="isDetails"></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="couponAmount" label="券抵扣金额(元)" width="150" align="center">
									<template slot-scope="scope">
										<el-input v-model="scope.row[scope.column.property]" style="width:100%" placeholder="请输入金额" :readonly="isDetails"></el-input>
									</template>
								</el-table-column>
								<el-table-column prop="status" label="状态" width="80" align="center">
									<template slot-scope="scope">
										<el-tag :ref="'statusTag' + scope.$index">{{ scope.row.status == 1 ? '上架' : '下架' }}</el-tag>
									</template>
								</el-table-column>
								
								<el-table-column fixed="right" prop="operate" label="操作" width="150" align="center" v-if="!isDetails">
									<template slot-scope="scope">
										<el-button type="warning" size="small" v-if="scope.row.status == 1" @click="offStatus(scope.row)">下架</el-button>
										<el-button type="success" size="small" v-if="scope.row.status == 0" @click="upStatus(scope.row)">上架</el-button>
										<el-button type="info" size="small" @click="handleDeleteSku(scope.$index, scope.row)">删除</el-button>
									</template>
								</el-table-column>
							</el-table>
						</div>
					</el-form-item>
				</el-col>
			</el-row>

			<!-- 自定义属性 -->
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="自定义属性">
						<el-button type="primary" plain @click="addSelfProperty" v-if="!isDetails">添加自定义属性</el-button>

						<!-- 自定义属性模板 -->
						<div v-if="selfPropertyVoList.length == 0 && isDetails">暂无属性</div>
						<div class="sku-list" v-for="(item, index) in selfPropertyVoList" :key="item.key">
							<!-- 属性名称 -->
							<div class="form-row">
								<el-form-item :label="'属性名称' + (index + 1)" label-width="80px">
									<el-input v-model="item.propertyName" style="width: 200px; margin-right: 10px;" placeholder="请输入自定义属性名称" @blur="enterSelfPropertyName(item)" :readonly="isDetails"></el-input>

									<el-button type="danger" icon="el-icon-delete" circle @click="delectSelfProperty(index)" v-if="!isDetails"></el-button>
								</el-form-item>
							</div>
							<!-- 属性值 -->
							<div>
								<el-form-item label="属性值" label-width="80px">
									<div class="sku-box">
										<el-tag
											v-for="(selfItem, selfIndex) of item.selfPropertyValueVoList"
											:key="selfIndex"
											:closable="!isDetails"
											:disable-transitions="false"
											@close="delectSelfSku(index, selfIndex)"
										>
											{{selfItem.propertyValue}}
										</el-tag>

										<div class="sku-input" v-if="item.showInput">
											<el-input :ref="'selfInput'+index" class="input-style" v-model="item.inputValue" placeholder="属性值" :readonly="isDetails" @blur="saveSelfSku(item)"></el-input>
										</div>

										<el-button class="button-style" size="small" @click="openSelfPropertyInput(item, index)" v-if="!item.showInput && !isDetails">添加自定义属性值</el-button>
									</div>
								</el-form-item>
							</div>
						</div>
					</el-form-item>
				</el-col>
			</el-row>

			<!-- 保存 -->
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item>
						<div class="form-submit">
							<el-button class="form-cancle-btn" :disabled="loading" @click="prev">上一步</el-button>
							<el-button class="form-submit-btn" :disabled="loading" type="primary" @click="onSubmit" v-if="!isDetails">保存</el-button>
							<el-button class="form-cancle-btn" :disabled="loading" @click="closePage" v-if="isDetails">返回</el-button>
							<el-button class="form-cancle-btn" :disabled="loading" @click="next">下一步</el-button>
						</div>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</div>
</template>

<script>
	/* eslint-disable */
	import util from '@/libs/util'
	import * as qiniu from 'qiniu-js'
	import { ossDomain } from '@api/sys.common';
	import { uploadImageUrl, getQiniuToken } from '@api/common/common'
	import { findProductSkuSelf, saveProductSkuSelf, addSkuProperty, delSkuProperty, addSkuPropertyValue, delSkuPropertyValue,  delSku, operateSkuStatus } from '@api/product/productSku'
	export default {
		props: {
			isDetails: {
				type: Boolean,
				default: false
			},
			productDetails: Object
		},
		data() {
			return {
				productId: '',
				skuDetails: {},
				skuVoListTemplate: [],
				
				addSkuPropertyList: [], //添加sku表格
				skuPropertyVoList: [], //sku规格列表
				skuVoList: [],
				selfPropertyVoList: [],
				
				fileList: [],
				isChoosed: 0,
				
				isTable: false,
				skuTableData: [],
				headerDatas: [],
				skuTableDataLength: [], //属性合并的长度
				lengthPos: 0, //属性合并的长度索引
				spanArr: [], //遍历数据时，根据相同的标识去存储记录
				pos: 0, // 二维数组的索引
					
				gotoNext: false,
				loading: false, //提交按钮
				
				headerParams: { //上传图片的请求头
					client_id: '',
					democracy: '',
				},

				imgUrlHeader: '', //图片地址
				
				//七牛云参数
				file: '',
				qiniuKey: '',
				qiniuToken: '',
				qiniuPutExtra: {},
				qiniuConfig: {
					useCdnDomain: true,
					retryCount: 6,
				},
			}
		},
		methods: {
			getSkuDetails() { //获取sku详情
				findProductSkuSelf({ productId: this.productId })
					.then(res => {
						//console.log(JSON.stringify(res.data))
						this.skuDetails = res.data;
						
						//sku列表
						this.addSkuPropertyList = this.skuDetails.skuPropertyVoList;
						this.skuPropertyVoList =  this.skuDetails.skuPropertyVoList;
						
						//sku组合列表
						this.skuDetails.skuVoList.forEach(item => {
							this.skuVoListTemplate.push(item);
						});
						this.selfPropertyVoList = this.skuDetails.selfPropertyVoList;
						
						//加自定义参数
						if(this.addSkuPropertyList.length > 0) {
							this.addSkuPropertyList.forEach(item => {
								this.$set(item, 'inputValue', '');
								this.$set(item, 'showInput', false);
							})
						}
						if(this.selfPropertyVoList.length > 0) {
							this.selfPropertyVoList.forEach(item => {
								this.$set(item, 'inputValue', '');
								this.$set(item, 'showInput', false);
							})
						}
						
						//table数据初始化
						if(this.skuPropertyVoList.length > 0) {
							this.initTableData();
							this.isTable = true;
						}
					})
			},
			
			//******添加规格******//
			addSkuProperty() { //显示添加输入框
				if(this.addSkuPropertyList.length == 2) {
					this.$message.warning('最多添加2组商品规格！');
					return;
				}
				if(this.addSkuPropertyList.length == 1 && !this.addSkuPropertyList[0].propertyName) {
					this.$message.warning('请先添加完第1组商品规格！');
					return;
				}
				
				this.addSkuPropertyList.push({
					propertyId: '',
					propertyName: '',
					skuPropertyValueVoList: [],
					inputValue: '',
					showInput: false,
				})
			},
			addPropertyName(item, index) { //添加规格名字
				if(this.isDetails || item.propertyId) {
					return;
				}
				if(!item.propertyName) {
					this.$message.warning('请输入规格名称！');
					return;
				}
				
				let params;
				if(item.propertyId) {
					params = {
						productId: this.productId,
						propertyId: item.propertyId,
						propertyName: item.propertyName
					}
				} else {
					params = {
						productId: this.productId,
						propertyName: item.propertyName
					}
				}
				
				addSkuProperty(params)
					.then(res => {
						item.propertyId = res.data.id;
						
						this.isTable = true;
						this.handleTableHeader(item, index);
						
						//console.log(JSON.stringify(this.skuPropertyVoList))
					})
			},
			delectPropertyName(item, index) { //删除规格名字
				this.$confirm('您是否确定删除此规格', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						delSkuProperty({
							id: item.propertyId
						})
							.then(res => {
								this.addSkuPropertyList.splice(index, 1);
								this.headerDatas.splice(index, 1);
							})
					})
					.catch(() => {
						
					})
			},
			
			openSkuInput(item, index) { //打开规格值输入框
				item.showInput = true;
				
				this.$nextTick(() => {
					this.$refs[`skuInput${index}`][0].$refs.input.focus();
				})
			},
			saveSku(item, index) { //保存规格值
				if(!item.inputValue) {
					this.$message.warning('请输入规格值！');
					return;
				}
				
				if(index == 0) {
					//规格1 需要先上传图片
					if (this.isChoosed == 1) {
						this.$refs[`skuImage${index}`][0].submit();
					} else if (!this.isChoosed) {
						this.$message.error('请选择规格值图片！');
						return;
					}
				} else {
					//规格2 直接保存
					addSkuPropertyValue({
						productId: this.productId,
						propertyId: item.propertyId,
						propertyValue: item.inputValue
					})
						.then(res => {
							item.skuPropertyValueVoList.push({
								productId: this.productId,
								propertyId: item.propertyId,
								propertyValueId: res.data.id,
								propertyValue: item.inputValue,
							});
							
							this.getSkuTable2(item); //更新表格
							
							//console.log('sku记录列表：添加规格2-----' + JSON.stringify(this.skuPropertyVoList));
						})
				}
			},
			delectSku(sku, index, skuIndex) { //删除规格值
				this.$confirm('您是否确定删除此规格值', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						delSkuPropertyValue({
							id: sku.propertyValueId
						})
							.then(() => {
								if(index == 0) {
									//删除规格1 的值
									let tag = 0;
									for(let i = 0; i < this.skuTableData.length; i++) {
										if(this.skuTableData[i].propertyValue1 == sku.propertyValue) {
											tag = i;
											break;
										}
									}
									this.skuTableData.splice(tag, this.skuTableDataLength[skuIndex])
									
									//确保sku列表的值同时被删除
									let tag2 = -1;
									for(let i = 0; i < this.skuVoList.length; i++) {
										if(this.skuVoList[i].propertyValue1 == sku.propertyValue) {
											tag2 = i;
											break;
										}
									}
									if(tag2 != -1) {
										this.skuVoList.splice(tag2, this.skuTableDataLength[skuIndex])
									}
									//console.log(JSON.stringify(this.skuVoList))
								} else {
									//删除规格2 的值
									this.skuTableData.forEach((list, idx) => {
										if(list.propertyValue2 == sku.propertyValue) {
											this.skuTableData.splice(idx, 1)
										}
									})
								}
								this.getSpanArr(this.skuTableData); //合并单元格
								
								this.addSkuPropertyList[index].skuPropertyValueVoList.splice(skuIndex, 1);
							})
					})
					.catch(() => {
						
					})
			},
			
			// 上传规格1的图片
			getUploadUrl() {
				return uploadImageUrl() + "?type=1"; //上传地址
			},
			checkPicture() {
				//选择图片
				if (this.isChoosed == 2) {
					this.isChoosed = 0;
				} else {
					this.isChoosed = 1;
				}
			},
			handleUploadSuccess(result, index) {
				// 上传成功回调
				if(result.code == '000000') {
					this.isChoosed = 2;
					
					addSkuPropertyValue({
						productId: this.productId,
						propertyId: this.addSkuPropertyList[index].propertyId,
						propertyValue: this.addSkuPropertyList[index].inputValue,
						thumbImage: result.data
					})
						.then(res => {
							this.addSkuPropertyList[index].skuPropertyValueVoList.push({
								productId: this.productId,
								propertyId: this.addSkuPropertyList[index].propertyId,
								propertyValueId: res.data.id,
								propertyValue: this.addSkuPropertyList[index].inputValue,
								thumbImage: result.data
							});
							
							this.getSkuTable1(this.addSkuPropertyList[index], result.data); //更新表格
							
							//清除数据
							this.addSkuPropertyList[index].inputValue = '';
							this.addSkuPropertyList[index].showInput = false;
							//console.log('sku记录列表：添加规格1-----' + JSON.stringify(this.skuTableData));
						})
				} else {
					this.$message.error(result.message)
				}
			},
			previewImage(file, fileList) {
				let size = file.size;
				if(size > 1024*1024*2) {
					this.$message.error('图片大小不能超过2M！');
					return false;
				}
			},
			handleRemove(file, fileList) {
				// 删除图片
				this.isChoosed = 0;
			},
			
			//七牛云上传
			getQiniuToken(file, index) { //获取七牛云token
				console.log(file)
				let name = file.file.name.split('.');
				let subffixName = '.' + name[name.length - 1];
				
				getQiniuToken({
						subffixName: subffixName
					})
					.then(res => {
						this.qiniuToken = res.data.token;
						this.qiniuKey = res.data.key;
						this.uploadFile(file, index);
					})
			},
			uploadFile(file, index) { //七牛云上传
				//qiniu.upload(file, key, token, putExtra, config)
				const observable = qiniu.upload(
					file.file,
					this.qiniuKey,
					this.qiniuToken,
					this.qiniuPutExtra,
					this.qiniuConfig
				)
			
				// 上传开始
				observable.subscribe({
					next: res => {
						console.log(res)
					},
					error: err => {
						console.log(err)
						this.handleRemove(file)
					},
					complete: res => {
						console.log(res)
						this.handleSuccess(process.env.VUE_APP_OSS_DOMAIN + res.key, index)
					},
				});
			},
			handleSuccess(result, index) { //七牛云上传成功
				this.isChoosed = 2;
				
				addSkuPropertyValue({
					productId: this.productId,
					propertyId: this.addSkuPropertyList[index].propertyId,
					propertyValue: this.addSkuPropertyList[index].inputValue,
					thumbImage: result
				})
					.then(res => {
						this.addSkuPropertyList[index].skuPropertyValueVoList.push({
							productId: this.productId,
							propertyId: this.addSkuPropertyList[index].propertyId,
							propertyValueId: res.data.id,
							propertyValue: this.addSkuPropertyList[index].inputValue,
							thumbImage: result
						});
						
						this.getSkuTable1(this.addSkuPropertyList[index], result); //更新表格
						
						//清除数据
						this.addSkuPropertyList[index].inputValue = '';
						this.addSkuPropertyList[index].showInput = false;
						//console.log('sku记录列表：添加规格1-----' + JSON.stringify(this.skuTableData));
					})
			},
			//******添加规格 END******//
			
			//******sku表格处理******//
			initTableData() { //初始化表格
				//表头部分
				this.skuPropertyVoList.forEach((item, index) => {
					this.headerDatas.push({
						prop: 'propertyValue' + (index+1),
						label: item.propertyName,
						width: '100'
					})
				});
				
				//表格内容
				//设定自定义参数
				//console.log(JSON.stringify(this.skuVoList))
				this.skuVoListTemplate.forEach(item => {
					this.$set(item, 'propertyValue1', item.propertyValueNames.split(',')[0]);
					this.$set(item, 'propertyValue2', item.propertyValueNames.split(',')[1]);
				});
				//重新排序，确保显示正确
				this.skuVoListTemplate = this.skuVoListTemplate.sort((a, b) => {
					return a.propertyValueIds.split(',')[0] - b.propertyValueIds.split(',')[0]
				});
				
				this.skuVoListTemplate.forEach(item => {
					this.skuTableData.push(item);
					this.skuVoList.push(item);
				})
				// this.skuTableData.forEach(item => {
				// 	this.$set(item, 'propertyValue1', item.propertyValueNames.split(',')[0]);
				// 	this.$set(item, 'propertyValue2', item.propertyValueNames.split(',')[1]);
				// });
				this.getSpanArr(this.skuTableData); //合并单元格
			},
			handleTableHeader(item, index) { //表头处理
				if(this.headerDatas.length == 0) {
					this.headerDatas.push({
						prop: 'propertyValue1',
						label: item.propertyName,
						width: '100'
					})
				} else if(this.headerDatas.length == 1) {
					if(index == 0) {
						this.headerDatas[0].label = item.propertyName
					} else {
						this.headerDatas.push({
							prop: 'propertyValue2',
							label: item.propertyName,
							width: '100'
						})
					}
				} else {
					if(index == 0) {
						this.headerDatas[0].label = item.propertyName
					} else {
						this.headerDatas[1].label = item.propertyName
					}
				}
			},
			
			getSkuTable1(sku, img) { //更新表单 添加规格1
				if(this.addSkuPropertyList.length > 1) {
					//有2组规格时
					if(this.addSkuPropertyList[1].skuPropertyValueVoList.length > 0) {
						//第二组规格值不为空
						for(let item of this.addSkuPropertyList[1].skuPropertyValueVoList) {
							let obj = {
								productId: this.productId,
								propertyIds: `${sku.propertyId},${item.propertyId}`,
								propertyValueIds: `${sku.skuPropertyValueVoList[sku.skuPropertyValueVoList.length-1].propertyValueId},${item.propertyValueId}`,
								propertyNames: `${sku.propertyName},${this.addSkuPropertyList[1].propertyName}`,
								propertyValueNames: `${sku.inputValue},${item.propertyValue}`,
								originalPropertyValueNames: `${sku.inputValue},${item.propertyValue}`,
								thumbImage: img,
								pricePurchase: '',
								priceShow: '',
								price: '',
								memberPrice: '',
								couponAmount: '',
								status: 1,
								productBarCode: '',
								skuName: `${sku.propertyName}:${sku.inputValue},${this.addSkuPropertyList[1].propertyName}:${item.propertyValue}`,
								propertyValue1: sku.inputValue,
								propertyValue2: item.propertyValue,
							}
							this.skuTableData.push(obj);
							this.skuVoList.push(obj);
						}
					} else {
						//第二组规格值为空
						let obj = {
							productId: this.productId,
							propertyIds: `${sku.propertyId}`,
							propertyValueIds: `${sku.skuPropertyValueVoList[sku.skuPropertyValueVoList.length-1].propertyValueId}`,
							propertyNames: `${sku.propertyName}`,
							propertyValueNames: `${sku.inputValue}`,
							originalPropertyValueNames: `${sku.inputValue}`,
							thumbImage: img,
							pricePurchase: '',
							priceShow: '',
							price: '',
							memberPrice: '',
							couponAmount: '',
							status: 1,
							productBarCode: '',
							skuName: `${sku.propertyName}:${sku.inputValue}`,
							propertyValue1: sku.inputValue,
							propertyValue2: '',
						}
						this.skuTableData.push(obj);
						this.skuVoList.push(obj);
					}
				} else {
					//只有1组规格
					let obj = {
						productId: this.productId,
						propertyIds: `${sku.propertyId}`,
						propertyValueIds: `${sku.skuPropertyValueVoList[sku.skuPropertyValueVoList.length-1].propertyValueId}`,
						propertyNames: `${sku.propertyName}`,
						propertyValueNames: `${sku.inputValue}`,
						originalPropertyValueNames: `${sku.inputValue}`,
						thumbImage: img,
						pricePurchase: '',
						priceShow: '',
						price: '',
						memberPrice: '',
						couponAmount: '',
						status: 1,
						productBarCode: '',
						skuName: `${sku.propertyName}:${sku.inputValue}`,
						propertyValue1: sku.inputValue,
					}
					this.skuTableData.push(obj);
					this.skuVoList.push(obj);
				}
				this.getSpanArr(this.skuTableData); //合并单元格
			},
			getSkuTable2(sku) { //更新表单 添加规格2
				let num = 0;
				this.skuTableDataLength.forEach((item, index) => {
					let obj = {
						productId: this.productId,
						propertyIds: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyId},${sku.propertyId}`,
						propertyValueIds: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValueId},${sku.skuPropertyValueVoList[sku.skuPropertyValueVoList.length-1].propertyValueId}`,
						propertyNames: `${this.addSkuPropertyList[0].propertyName},${sku.propertyName}`,
						propertyValueNames: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${sku.inputValue}`,
						originalPropertyValueNames: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${sku.inputValue}`,
						thumbImage: this.skuPropertyVoList[0].skuPropertyValueVoList[index].thumbImage,
						pricePurchase: '',
						priceShow: '',
						price: '',
						memberPrice: '',
						couponAmount: '',
						status: 1,
						productBarCode: '',
						propertyValue1: this.skuPropertyVoList[0].skuPropertyValueVoList[index].propertyValue,
						propertyValue2: sku.inputValue,
					}
					if(index == 0) {
						num = num + item;
					} else {
						num = num + item + 1;
					}
					if(item == 1 && !this.skuTableData[index].propertyValue2) {
						//判断表格中的规格2是不是为空
						//如果为空要先补第一行
						this.skuTableData[index].propertyIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyId},${sku.propertyId}`;
						this.skuTableData[index].propertyValueIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValueId},${sku.skuPropertyValueVoList[sku.skuPropertyValueVoList.length-1].propertyValueId}`;
						this.skuTableData[index].propertyNames = `${this.addSkuPropertyList[0].propertyName},${sku.propertyName}`;
						this.skuTableData[index].propertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${sku.inputValue}`;
						this.skuTableData[index].originalPropertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${sku.inputValue}`;
						this.$set(this.skuTableData[index], 'propertyValue2', sku.inputValue);
						
						this.skuVoList[index].propertyIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyId},${sku.propertyId}`;
						this.skuVoList[index].propertyValueIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValueId},${sku.skuPropertyValueVoList[sku.skuPropertyValueVoList.length-1].propertyValueId}`;
						this.skuVoList[index].propertyNames = `${this.addSkuPropertyList[0].propertyName},${sku.propertyName}`;
						this.skuVoList[index].propertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${sku.inputValue}`;
						this.skuVoList[index].originalPropertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${sku.inputValue}`;
						this.$set(this.skuVoList[index], 'propertyValue2', sku.inputValue);
					} else {
						this.skuTableData.splice(num, 0, obj);
						this.skuVoList.splice(num, 0, obj);
					}
				});
				
				this.getSpanArr(this.skuTableData);
				sku.inputValue = '';
				sku.showInput = false;
				//console.log('----skuVoList2222----' + JSON.stringify(this.skuVoList));
				//console.log(JSON.stringify(this.skuTableData));
			},
			
			getSpanArr(arr) { //设置合并单元格标志，和列表属性长度
				this.spanArr = []; //合并单元格标识数组
				this.skuTableDataLength = []; //规格1长度数组
				arr.forEach((item, index) => {
					//判断是否是第一项
					if (index === 0) {
						this.spanArr.push(1);
						this.skuTableDataLength.push(1)
						
						this.pos = 0;
						this.lengthPos = 0;
					} else {
						//不是第一项时，就根据标识去存储
						if (arr[index].propertyValue1 === arr[index - 1].propertyValue1) {
							this.spanArr[this.pos] += 1;
							this.spanArr.push(0);
							
							this.skuTableDataLength[this.lengthPos] += 1;
						} else {
							// 没有符合的数据时，要记住当前的index
							this.spanArr.push(1);
							this.pos = index;
							
							this.skuTableDataLength.push(1);
							this.lengthPos += 1;
						}
					}
				});
				//console.log('skuTableDataLength' + JSON.stringify(this.skuTableDataLength))
			},
			objectSpanMethod({rowIndex, columnIndex}) { //合并单元格方法
				if(columnIndex === 0) { //合并第一列
					const _row = this.spanArr[rowIndex];
					const _col = _row > 0 ? 1 : 0;
					// [0,0] 表示这一行不显示， [2,1]表示行的合并数
					return {
						rowspan: _row,
						colspan: _col
					};
				}
				if (columnIndex === 1) { //合并第二列
					const _row = this.spanArr[rowIndex];
					const _col = _row > 0 ? 1 : 0;
					// [0,0] 表示这一行不显示， [2,1]表示行的合并数
					return {
						rowspan: _row,
						colspan: _col
					};
				}
			},
			
			handleDeleteSku(index, row) { //删除列表
				// if (this.skuVoList.length == 1) {
				// 	this.$message.warning('只剩下一条SKU，不允许删除了！');
				// 	return;
				// }
				
				this.$confirm('您是否确认删除SKU【'+ row.propertyValue1 + (row.propertyValue2==undefined?'':row.propertyValue2) +'】?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						if(row.skuId) {
							// 异步实时删除
							delSku({ id: row.skuId })
								.then(res => {
									this.$message.success('删除成功');
									
									//删除表格里对应的sku
									this.skuTableData.splice(index, 1);
									
									//删除sku列表对应的sku
									let idx = 0;
									this.skuVoList.forEach((item, idx2) => {
										if(item.skuId == row.skuId) {
											idx = idx2;
										}
									});
									this.skuVoList.splice(idx, 1);
								})
								.catch(() => {
									
								})
						} else {
							//删除表格里对应的sku
							this.skuTableData.splice(index, 1);
							
							//删除sku列表对应的sku
							let idx = 0;
							this.skuVoList.forEach((item, idx2) => {
								if(item.propertyValueNames == row.propertyValueNames) {
									idx = idx2;
								}
							});
							this.skuVoList.splice(idx, 1);
						}
						this.getSpanArr(this.skuTableData);
					})
					.catch(() => {
						
					})
			},
			//******sku表格处理 END******//
			
			//******sku表格刷新******//
			refreshTableData() {
				//有2组规格时
				//先处理第2组规格
				if(this.addSkuPropertyList.length > 1) {
					//先判断表格中是第2组规格值是否为空
					this.skuTableDataLength.forEach((item, index) => {
						if(item == 1 && !this.skuTableData[index].propertyValue2) {
							//如果为空要先补第一行
							this.skuTableData[index].propertyIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyId},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyId}`;
							this.skuTableData[index].propertyValueIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValueId},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValueId}`;
							this.skuTableData[index].propertyNames = `${this.addSkuPropertyList[0].propertyName},${this.addSkuPropertyList[1].propertyName}`;
							this.skuTableData[index].propertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].inputValue}`;
							this.skuTableData[index].originalPropertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValue}`;
							this.$set(this.skuTableData[index], 'propertyValue2', this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValue);
							
							this.skuVoList[index].propertyIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyId},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyId}`;
							this.skuVoList[index].propertyValueIds = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValueId},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValueId}`;
							this.skuVoList[index].propertyNames = `${this.addSkuPropertyList[0].propertyName},${this.addSkuPropertyList[1].propertyName}`;
							this.skuVoList[index].propertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValue}`;
							this.skuVoList[index].originalPropertyValueNames = `${this.addSkuPropertyList[0].skuPropertyValueVoList[index].propertyValue},${this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValue}`;
							this.$set(this.skuVoList[index], 'propertyValue2', this.addSkuPropertyList[1].skuPropertyValueVoList[0].propertyValue);
						}
					})
					//再遍历规格2，把剩下的加进去
					this.addSkuPropertyList[1].skuPropertyValueVoList.forEach((item1, index1) => {
						this.addSkuPropertyList[0].skuPropertyValueVoList.forEach((item2, index2) => {
							if(!this.checkSecondProperty(`${item2.propertyValueId},${item1.propertyValueId}`)) {
								this.addSecondProperty(index2, index1);
							}
						})
					})
				}
				
				//最后处理第1组规格
				if(this.addSkuPropertyList.length > 0) {
					this.addSkuPropertyList[0].skuPropertyValueVoList.forEach((item, index) => {
						if(!this.checkFirstProperty(item.propertyValueId)) {
							this.addFirstProperty(this.addSkuPropertyList[0], index);
						}
					})
				}
			},
			
			checkFirstProperty(id) { //检查列表里是否已经有第1个规格值
				let isCheck = false; //默认没有
				
				for(let i = 0; i < this.skuTableData.length; i++) {
					let propertyId = 0;
					if(this.skuTableData[i].propertyValueIds.indexOf(',') != -1) {
						propertyId = this.skuTableData[i].propertyValueIds.split(',')[0];
					} else {
						propertyId = this.skuTableData[i].propertyValueIds;
					}
					if(parseInt(propertyId) == id) {
						isCheck = true;
						break;
					}
				}
				return isCheck;
			},
			addFirstProperty(sku, index) { //插入第1组规格
				if(this.addSkuPropertyList.length > 1) {
					//有2组规格时
					if(this.addSkuPropertyList[1].skuPropertyValueVoList.length > 0) {
						//第二组规格值不为空
						for(let item of this.addSkuPropertyList[1].skuPropertyValueVoList) {
							let obj = {
								productId: this.productId,
								propertyIds: `${sku.propertyId},${item.propertyId}`,
								propertyValueIds: `${sku.skuPropertyValueVoList[index].propertyValueId},${item.propertyValueId}`,
								propertyNames: `${sku.propertyName},${this.addSkuPropertyList[1].propertyName}`,
								propertyValueNames: `${sku.skuPropertyValueVoList[index].propertyValue},${item.propertyValue}`,
								originalPropertyValueNames: `${sku.skuPropertyValueVoList[index].propertyValue},${item.propertyValue}`,
								thumbImage: sku.skuPropertyValueVoList[index].thumbImage,
								pricePurchase: '',
								priceShow: '',
								price: '',
								memberPrice: '',
								couponAmount: '',
								status: 1,
								productBarCode: '',
								skuName: `${sku.propertyName}:${sku.skuPropertyValueVoList[index].propertyValue},${this.addSkuPropertyList[1].propertyName}:${item.propertyValue}`,
								propertyValue1: sku.skuPropertyValueVoList[index].propertyValue,
								propertyValue2: item.propertyValue,
							}
							this.skuTableData.push(obj);
							this.skuVoList.push(obj);
						}
					} else {
						//第二组规格值为空
						let obj = {
							productId: this.productId,
							propertyIds: `${sku.propertyId}`,
							propertyValueIds: `${sku.skuPropertyValueVoList[index].propertyValueId}`,
							propertyNames: `${sku.propertyName}`,
							propertyValueNames: `${sku.skuPropertyValueVoList[index].propertyValue}`,
							originalPropertyValueNames: `${sku.skuPropertyValueVoList[index].propertyValue}`,
							thumbImage: sku.skuPropertyValueVoList[index].thumbImage,
							pricePurchase: '',
							priceShow: '',
							price: '',
							memberPrice: '',
							couponAmount: '',
							status: 1,
							productBarCode: '',
							skuName: `${sku.propertyName}:${sku.skuPropertyValueVoList[index].propertyValue}`,
							propertyValue1: sku.propertyValue,
							propertyValue2: '',
						}
						this.skuTableData.push(obj);
						this.skuVoList.push(obj);
					}
				} else {
					//只有1组规格
					let obj = {
						productId: this.productId,
						propertyIds: `${sku.propertyId}`,
						propertyValueIds: `${sku.skuPropertyValueVoList[index].propertyValueId}`,
						propertyNames: `${sku.propertyName}`,
						propertyValueNames: `${sku.skuPropertyValueVoList[index].propertyValue}`,
						originalPropertyValueNames: `${sku.skuPropertyValueVoList[index].propertyValue}`,
						thumbImage: sku.skuPropertyValueVoList[index].thumbImage,
						pricePurchase: '',
						priceShow: '',
						price: '',
						memberPrice: '',
						couponAmount: '',
						status: 1,
						productBarCode: '',
						skuName: `${sku.propertyName}:${sku.skuPropertyValueVoList[index].propertyValue}`,
						propertyValue1: sku.skuPropertyValueVoList[index].propertyValue,
					}
					this.skuTableData.push(obj);
					this.skuVoList.push(obj);
				}
				
				this.getSpanArr(this.skuTableData); //合并单元格
			},
			
			checkSecondProperty(ids) { //检查列表里是否已经有第2个规格值
				let isCheck = false; //默认没有
				
				for(let i = 0; i < this.skuTableData.length; i++) {
					if(this.skuTableData[i].propertyValueIds == ids) {
						isCheck = true;
						break;
					}
				}
				return isCheck;
			},
			addSecondProperty(index1, index2) { //插入第2组规格
				let obj = {
					productId: this.productId,
					propertyIds: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index1].propertyId},${this.addSkuPropertyList[1].skuPropertyValueVoList[index2].propertyId}`,
					propertyValueIds: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index1].propertyValueId},${this.addSkuPropertyList[1].skuPropertyValueVoList[index2].propertyValueId}`,
					propertyNames: `${this.addSkuPropertyList[0].propertyName},${this.addSkuPropertyList[1].propertyName}`,
					propertyValueNames: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index1].propertyValue},${this.addSkuPropertyList[1].skuPropertyValueVoList[index2].propertyValue}`,
					originalPropertyValueNames: `${this.addSkuPropertyList[0].skuPropertyValueVoList[index1].propertyValue},${this.addSkuPropertyList[1].skuPropertyValueVoList[index2].propertyValue}`,
					thumbImage: this.addSkuPropertyList[0].skuPropertyValueVoList[index1].thumbImage,
					pricePurchase: '',
					priceShow: '',
					price: '',
					memberPrice: '',
					couponAmount: '',
					status: 1,
					productBarCode: '',
					propertyValue1: this.addSkuPropertyList[0].skuPropertyValueVoList[index1].propertyValue,
					propertyValue2: this.addSkuPropertyList[1].skuPropertyValueVoList[index2].propertyValue,
				}
				
				this.skuTableData.push(obj);
				this.skuVoList.push(obj);
				
				//重新排序，确保显示正确
				this.skuTableData = this.skuTableData.sort((a, b) => {
					return a.propertyValueIds.split(',')[0] - b.propertyValueIds.split(',')[0]
				});
				
				this.getSpanArr(this.skuTableData);
				//console.log('----skuVoList2222----' + JSON.stringify(this.skuVoList));
			},
			//******sku表格刷新 END******//
			
			//******批量设置******//
			openBatchPricePurchase() { //批量设置参考进货价
				if (this.skuTableData.length < 1) {
					this.$message.warning('请先添加规格值');
					return;
				}
				
				this.$prompt('批量设置参考进货价', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					inputPlaceholder: '请输入参考进货价',
					inputPattern: /^(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/,
					inputErrorMessage: '价格格式不正确！'
				})
					.then(({ value }) => {
						this.skuTableData.forEach(item => {
							item.pricePurchase = value;
						});
						this.$message.success('添加成功！')
					})
			},
			openBatchPriceShow() { //批量设置原价
				if (this.skuTableData.length < 1) {
					this.$message.warning('请先添加规格值');
					return;
				}
				
				this.$prompt('批量设置原价', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					inputPlaceholder: '请输入原价',
					inputPattern: /^(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/,
					inputErrorMessage: '价格格式不正确！'
				})
					.then(({ value }) => {
						this.skuTableData.forEach(item => {
							item.priceShow = value;
						});
						this.$message.success('添加成功！')
					})
			},
			openBatchPrice() { //批量设置销售价
				if (this.skuTableData.length < 1) {
					this.$message.warning('请先添加规格值');
					return;
				}
				
				this.$prompt('批量设置销售价', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					inputPlaceholder: '请输入销售价',
					inputPattern: /^(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/,
					inputErrorMessage: '价格格式不正确！'
				})
					.then(({ value }) => {
						this.skuTableData.forEach(item => {
							item.price = value;
						});
						this.$message.success('添加成功！')
					})
			},
			openBatchMemberPrice() { //批量设置会员价
				if (this.skuTableData.length < 1) {
					this.$message.warning('请先添加规格值');
					return;
				}
				
				this.$prompt('批量设置会员价', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					inputPlaceholder: '请输入会员价',
					inputPattern: /^(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/,
					inputErrorMessage: '价格格式不正确！'
				})
					.then(({ value }) => {
						this.skuTableData.forEach(item => {
							item.memberPrice = value;
						});
						this.$message.success('添加成功！')
					})
			},
			openBatchCouponAmount() { //批量设置券抵扣金额
				if (this.skuTableData.length < 1) {
					this.$message.warning('请先添加规格值');
					return;
				}
				
				this.$prompt('批量设置券抵扣金额', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					inputPlaceholder: '请输入券抵扣金额',
					inputPattern: /^(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/,
					inputErrorMessage: '价格格式不正确！'
				})
					.then(({ value }) => {
						this.skuTableData.forEach(item => {
							item.couponAmount = value;
						});
						this.$message.success('添加成功！')
					})
			},
			//******批量设置 END******//
			
			//******自定义属性 END******//
			addSelfProperty() { //添加自定义属性按钮
				this.selfPropertyVoList.push({
					propertyName: '',
					selfPropertyValueVoList: [],
					inputValue: '',
					showInput: false,
				})
			},
			enterSelfPropertyName(item) { //属性名字输入框
				if(!item.propertyName) {
					this.$message.warning('请输入自定义属性名称！');
					return;
				}
			},
			delectSelfProperty(index) { //删除自定义属性名字
				this.$confirm('您是否确定删除此自定义属性', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						this.selfPropertyVoList.splice(index, 1)
					})
			},
			delectSelfSku(index, itemIndex) { //删除自定义属性值
				this.$confirm('您是否确定删除此自定义属性值', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				})
					.then(() => {
						this.selfPropertyVoList[index].selfPropertyValueVoList.splice(itemIndex, 1);
						// console.log(JSON.stringify(this.selfPropertyVoList))
					})
			},
			
			openSelfPropertyInput(item, index) { //属性值输入框
				item.showInput = true;
				
				this.$nextTick(() => {
					this.$refs[`selfInput${index}`][0].$refs.input.focus();
				})
			},
			saveSelfSku(item) { //保存属性值
				if(this.isDetails) {
					return;
				}
				item.selfPropertyValueVoList.push({
					productId: this.productId,
					propertyValue: item.inputValue,
				});
				item.inputValue = '';
				item.showInput = false;
			},
			//******自定义属性 END******//
			
			//******提交表单******//
			onSubmit() {
				//console.log('sku参数列表' + JSON.stringify(this.skuVoList))
				//console.log('sku显示表格列表' + JSON.stringify(this.skuTableData))
				if(this.skuPropertyVoList.length == 0) {
					this.$message.warning('请先添加规格！');
					return;
				}
				for (let i in this.skuPropertyVoList) {
					if(!this.skuPropertyVoList[i].propertyName) {
						this.$message.warning('规格名称不能为空！');
						return;
					}
					if (this.skuPropertyVoList[i].skuPropertyValueVoList.length == 0) {
						this.$message.warning('请先添加规格值！');
						return;
					}
				}
				if(this.skuVoList.length == 0) {
					this.$message.warning('请先添加SKU');
					return;
				}
				for (let i in this.skuVoList) {
					if (this.skuVoList[i].productBarCode === '') {
						this.$message.warning('条码不能为空！');
						return;
					}
					if (this.skuVoList[i].productBarCode.length > 18) {
						this.$message.warning('条码长度不允许超过18位！');
						return;
					}
					let regExp = /^(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
					if (this.skuVoList[i].pricePurchase === '') {
						this.$message.warning('参考进货价不能为空！');
						return false;
					}
					if (!regExp.test(this.skuVoList[i].pricePurchase)) {
						this.$message.warning('参考进货价格式不正确！');
						return false;
					}
					if (this.skuVoList[i].priceShow === '') {
						this.$message.warning('原价不能为空！');
						return false;
					}
					if (!regExp.test(this.skuVoList[i].priceShow)) {
						this.$message.warning('原价格式不正确！');
						return false;
					}
					if (this.skuVoList[i].price === '') {
						this.$message.warning('销售价不能为空！');
						return false;
					}
					if (!regExp.test(this.skuVoList[i].price)) {
						this.$message.warning('销售价格式不正确！');
						return false;
					}
					if (this.skuVoList[i].memberPrice === '') {
						this.$message.warning('会员价不能为空！');
						return false;
					}
					if (!regExp.test(this.skuVoList[i].memberPrice)) {
						this.$message.warning('会员价格式不正确！');
						return false;
					}
					if (this.skuVoList[i].couponAmount === '') {
						this.$message.warning('券抵扣金额不能为空！');
						return false;
					}
					if (!regExp.test(this.skuVoList[i].couponAmount)) {
						this.$message.warning('券抵扣金额格式不正确！');
						return false;
					}
				}
				
				//处理参数
				//处理规格列表
				let skuList = this.objDeepCopy(this.skuPropertyVoList);
				skuList.forEach(item => {
					this.$delete(item, 'inputValue');
					this.$delete(item, 'showInput');
				});
				
				//处理规格sku组合列表
				let skuList2 = this.objDeepCopy(this.skuVoList);
				skuList2.forEach(item => {
					this.$delete(item, 'propertyValue1');
					this.$delete(item, 'propertyValue2');
				});
				
				//处理自定义规格列表
				let selfList = this.objDeepCopy(this.selfPropertyVoList);
				selfList.forEach(item => {
					this.$delete(item, 'inputValue');
					this.$delete(item, 'showInput');
				});
				
				//最终参数
				let params = {
					productId: this.productId,
					skuPropertyVoList: skuList,
					skuVoList: skuList2,
					selfPropertyVoList: selfList
				}
				this.loading = true;
				
				saveProductSkuSelf(params)
					.then(() => {
						this.$message.success('保存成功');
						this.loading = false;
						
						if (this.gotoNext) {
							this.$emit('next', 2);
						} else {
							this.$emit('close');
						}
					})
					.catch(() => {
						this.gotoNext = false
						this.loading = false;
					})
			},
			next() {
				// 下一步
				if(this.isDetails) {
					this.$emit('next', 2)
				} else {
					this.gotoNext = true;
					this.onSubmit();
				}
			},
			prev() {
				// 上一步
				this.$emit('step', 0);
			},
			closePage() {
				//返回列表
				this.$emit('close');
			},
			offStatus(item) {
				if(item.skuId === '' || item.skuId === undefined) {
					item.status = 0;
					return false;
				}
				operateSkuStatus({ skuId: item.skuId, status: 0 })
				.then(() => {
					this.$message.success('下架成功！');
					item.status = 0;
				})
			},
			upStatus(item) {
				if(item.skuId === '' || item.skuId === undefined) {
					item.status = 1
					return false
				}
				operateSkuStatus({ skuId: item.skuId, status: 1 })
				.then(() => {
					this.$message.success('上架成功！');
					item.status = 1;
				})
			},
			
			objDeepCopy (source) { //深拷贝数组or对象
				let sourceCopy = source instanceof Array ? [] : {};
				for (let item in source) {
					sourceCopy[item] = typeof source[item] === 'object' ? this.objDeepCopy(source[item]) : source[item];
				}
				return sourceCopy;
			}
		},
		mounted() {
			if (sessionStorage.getItem('productId')) {
				this.productId = parseInt(sessionStorage.getItem('productId'));
				this.getSkuDetails();
			}
			
			//上传图片插件的请求头
			let getId = util.cookies.get('client');
			let client_id;
			let getId2_reverse = getId.substring(0, 6);
			let getId2 = getId2_reverse.split('').reverse().join(''); //后6位
			let getId1 = getId.substring(6, 13); //前7位
			client_id = 'client' + getId1 + getId2;
			
			let democracy = util.cookies.get('democracy');
			
			this.headerParams.clientId = client_id;
			this.headerParams.democracy = democracy;

			this.imgUrlHeader = ossDomain;
		}
	};
</script>

<style lang="scss" scoped>
	.sku-list {
		width: 100%;
		padding: 22px 0 0 0;
		::v-deep .el-tag {
			align-items: center;
			display: flex;
		}
		::v-deep .el-tag .el-image {
			margin-left: 4px;
		}
		::v-deep .el-tag:first-child {
			margin-left: 0;
		}
	}
	.form-row {
		margin-bottom: 12px;
	}

	.input-style {
		width: 90px;
		height: 32px;
		margin-left: 10px;
		&:first-child {
			margin-left: 10px;
		}
	}
	.input-style ::v-deep .el-input__inner {
		height: 32px;
		line-height: 32px;
	}
	.button-style {
		margin-left: 10px;
	}
	.button-style:first-child {
		margin-left: 0;
	}

	.sku-box {
		line-height: 32px;
		display: flex;
		margin-top: 4px;

		.sku-tag {
			height: 32px;
			align-items: center;
			display: flex;
		}

		.sku-input {
			line-height: 32px;
			display: flex;

			.sku-pic-upload {
				width: 32px;
				height: 32px;
				margin-left: 10px;
				position: relative;
				::v-deep .el-upload {
					width: 32px;
					height: 32px;
					font-size: 20px;
					background: none;
					border: 0;
					justify-content: center;
					align-items: center;
					display: flex;
				}
			}
		}
	}

	.sku-box .el-tag{
		margin-left: 10px;
	}

	::v-deep .el-upload-list--picture-card {
		position: absolute;
		left: 0;
		top: 0;
	}
	::v-deep .el-upload-list--picture-card .el-upload-list__item {
		width: 32px;
		height: 32px;
		border: 0;
		border-radius: 0;
		margin: 0;
	}

	.el-alert {
		width: auto;
		padding: 0 20px 0 6px;
	}

	.table-header {
		justify-content: space-between;
		display: flex;
		&>div {
			display: flex;
		}
	}
</style>
